package com.webcams.ui.downloadmaps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.webcams.core.download.DownloadMapsManager;
import com.webcams.core.download.IDownloadMapListChangedListener;
import com.webcams.core.resorts.Resort;
import com.webcams.ui.R;

public class DownloadMapsLocalPartUI extends ListActivity implements IDownloadMapListChangedListener {

	private ArrayList<Resort> currentList;

	@Override
	public void onCreate(Bundle state) {

		super.onCreate(state);
		setContentView(R.layout.download_maps_dist);
		ListView lv = getListView();

		currentList = new ArrayList<Resort>();
		listChanged();

		lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> av, View v, int position, long arg3) {
				clickedItem(currentList.get(position));
			}			
		});

		lv.setTextFilterEnabled(true);

		DownloadMapsManager.getInstance(this).addListChangedListener(this);

	}

	private void clickedItem(final Resort r) {
		final CharSequence[] items = {getResources().getString(R.string.downloadmaps_localmaps_update), 
				getResources().getString(R.string.downloadmaps_localmaps_delete)};
		final DownloadMapsManager mapManager = DownloadMapsManager.getInstance(this);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(r.getName());
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				if(item == 0) {
					final ProgressDialog progressDialog = new ProgressDialog(DownloadMapsLocalPartUI.this);
					progressDialog.setCancelable(false);
					progressDialog.setMessage(getResources().getString(R.string.downloadmaps_localmaps_downloadprogress));
					progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
					progressDialog.setProgress(0);
					progressDialog.show();
					mapManager.addOrUpdateResort(r, progressDialog);
				} else {
					mapManager.deleteResort(r);
					listChanged();
				}
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public void listChanged() {
		if(currentList != null){
			currentList.clear();

			DownloadMapsManager mapManager = DownloadMapsManager.getInstance(this);

			for(Resort r : mapManager.getResortsDownloaded()) {
				currentList.add(r);
			}
			

			String[] listNames = new String[currentList.size()];
			int i = 0;
			for(Resort r : currentList){
				String prefix = new String();
				if(r.newMapAvailable(this)) {
					prefix = getResources().getString(R.string.downloadmaps_localmaps_updatenotif)+" ";
				}
				
				listNames[i++] = prefix+r.getName()+" ("+(r.getMapSize()/1024)+" Ko)";
			}
			
			
			setListAdapter(new ArrayAdapter<String>(this, 
					android.R.layout.simple_list_item_1,
					listNames));
		}
	}

}
